package com.jiukuaidao.library.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiukuaidao.library.R;
import com.jiukuaidao.library.util.Utils;


/**
 * Author : BinarySatan
 * Date : 2016/5/26
 * Description : 加载对话框
 */
public class LoadingDialog extends Dialog {

    private AnimationDrawable _ad;


    public LoadingDialog(Context context) {
        this(context, R.style.commDialogStyle);
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    private void init() {
        this.setCanceledOnTouchOutside(false);
    }


    public void show(String msg) {
        if (isShowing())
            return;

        View view = LayoutInflater.from(Utils.getContext()).inflate(R.layout.common_dialog_loading, new FrameLayout(getContext()));
        ImageView ivLoading = (ImageView) view.findViewById(R.id.iv_loading);

        if (!TextUtils.isEmpty(msg)) {
            TextView tvMessage = (TextView) view.findViewById(R.id.tv_message);
            tvMessage.setText(msg);
            tvMessage.setVisibility(View.VISIBLE);
        }

        _ad = (AnimationDrawable) ivLoading.getDrawable();
        _ad.start();
        setContentView(view);

        super.show();
    }


    public void dismiss() {
        if (this.isShowing()) {

            if (_ad != null)
                _ad.stop();

            super.dismiss();
        }
    }
}
